package com.leyou.user.service;

import com.leyou.common.pojo.NumberUtils;
import com.leyou.user.pojo.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.utils.CodecUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    private static final String KEY_PREFIX = "user:verify:";

    @Autowired
    private StringRedisTemplate redisTemplate;

    public Boolean checkUserData(String data, Integer type) {

        User user = new User();
        switch (type) {
            case 1:
                user.setUsername(data);
                break;
            case 2:
                user.setPhone(data);
                break;
            default:
                return null;
        }
        int count = userMapper.selectCount(user);
        return count==0;
    }


    /**
     * 发送验证码
     * @param phone
     */
    public void sendVerifyCode(String phone) {

        if (StringUtils.isEmpty(phone)) {
            return;
        }

        // 生成验证码
        String code = NumberUtils.generateCode(6);

        // 发送消息到Ribbitmq
        Map<String, String> msg = new HashMap<>();
        try {
            msg.put("phone", phone);
            msg.put("code", code);
            amqpTemplate.convertAndSend("LEYOU.SMS.EXCHANGE","sms.verify.code",msg);
            // 将验证码放入到redis中
            redisTemplate.opsForValue().set(KEY_PREFIX+phone,code,10, TimeUnit.MINUTES);
        } catch (AmqpException e) {
            e.printStackTrace();
        }

    }

    /**
     * 注册
     * @param user
     * @param code
     * @return
     */
    public Boolean register(User user, String code) {

        // 校验短信验证码
        String redisCode = redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
        if (!StringUtils.equals(code,redisCode)) {
            return false;
        }

        // 生成盐 并设置盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        // 对密码加密
        user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));

        // 强制设置不能指定的参数为null
        user.setId(null);
        user.setCreated(new Date());

        // 添加到数据库
        int insert = userMapper.insert(user);

        //Boolean boo = userMapper.insertSelective(user) == 1;
        // 注册成功 删除数据库中的记录
        if (insert==1) {
            redisTemplate.delete(KEY_PREFIX + user.getPhone());
            return true;
        }
        return false;

    }

    /**
     * 根据用户名和密码进行查询
     */
    public User queryUser(String username, String password) {

        // 查询用户
        User user = new User();
        user.setUsername(username);
        User user1 = userMapper.selectOne(user);

        // 校验用户名
        if (user1 == null) {
            return null;
        }
        // 校验密码
        if (!user1.getPassword().equals(CodecUtils.md5Hex(password,user1.getSalt()))) {
            return null;
        }
        return user1;
    }
}
